﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MouseFilter.cs" company="">
//   
// </copyright>
// <summary>
//   The mouse message filter.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Core
{
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    /// <summary>
    /// The mouse message filter.
    /// </summary>
    public class MouseMessageFilter : IMessageFilter
    {
        #region Constants

        /// <summary>
        /// The w m_ mousemove.
        /// </summary>
        private const int WM_MOUSEMOVE = 0x0200;

        /// <summary>
        /// The w m_ mousewheel.
        /// </summary>
        private const int WM_MOUSEWHEEL = 0x020a;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes static members of the <see cref="MouseMessageFilter"/> class.
        /// </summary>
        static MouseMessageFilter()
        {
            Instance = new MouseMessageFilter();
            Instance.Enabled = true;
            Application.AddMessageFilter(Instance);
        }

        #endregion

        #region Public Events

        /// <summary>
        /// The mouse move.
        /// </summary>
        public event Action MouseMove;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static MouseMessageFilter Instance { get; private set; }

        /// <summary>
        /// Gets or sets a value indicating whether enabled.
        /// </summary>
        public bool Enabled { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The pre filter message.
        /// </summary>
        /// <param name="m">
        /// The m.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_MOUSEMOVE)
            {
                if (Enabled && MouseMove != null)
                {
                    MouseMove();
                }
            }

            // if (m.Msg == WM_MOUSEWHEEL)
            // {
            // 	SendMessage((int)m_control.Handle, m.Msg, (Int32)m.WParam, (Int32)m.LParam);
            // }
            return false;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The send message.
        /// </summary>
        /// <param name="hWnd">
        /// The h wnd.
        /// </param>
        /// <param name="msg">
        /// The msg.
        /// </param>
        /// <param name="wp">
        /// The wp.
        /// </param>
        /// <param name="lp">
        /// The lp.
        /// </param>
        /// <returns>
        /// The <see cref="IntPtr"/>.
        /// </returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

        #endregion
    }
}